//Zombie behavior routines written by Celery


private ["_radius","_victimradius","_bandage","_mag","_sound","_noisewait","_movepos","_wait","_dist","_nearest","_victim","_victims","_newmovedist","_victimchangecooldown","_anim","_claw","_object","_damage","_randomradius","_newmovepos","_action","_leftstring","_h","_zombie","_zombietype","_armored","_maxradius"];
_zombie=_this select 0;
_zombietype=_this select 1;
_radius=_this select 2;
_wait=time+random 35;
_victimchangecooldown=time;
_victim=objNull;
_noisewait=5+random 40;
_armored=_zombietype in ["armored","slow armored"] or typeOf _zombie in CLY_armoredzombieclasses;

//Aggro radius
_maxradius=if ({_zombie distance _x<=CLY_altmaxaggroradiusrange} count CLY_altmaxaggroradiusobjects==0) then {CLY_maxaggroradius} else {CLY_altmaxaggroradius};
if (_radius==0) then {_radius=CLY_minaggroradius+random (_maxradius-CLY_minaggroradius)};
if (_zombietype in ["crawler","sneaker"]) then {_radius=_maxradius};
_victimradius=CLY_randomvictimradius;
if (_victimradius>0 and _victimradius<=1) then {_victimradius=_radius*_victimradius};

//Killed EH
_zombie addEventHandler [
	"Killed",
	{
		if (random 1<0.4) then {
			_zombie=_this select 0;
			_sound=CLY_noises_die select floor random count CLY_noises_die;
			_object="HeliHEmpty" createVehicleLocal [0,0,0];
			_object attachTo [_zombie,[0,0,1.5]];
			_object say3D _sound;
			[_object] spawn {sleep 5;deleteVehicle (_this select 0)};
			CLY_z_attackpv=[_zombie,_sound];
			publicVariable "CLY_z_attackpv";
		};
	}
];

//Determine whether zombie carries a bandage
_bandage=false;
if (random 1<=CLY_zombiebandagechance or _armored) then {
	_bandage=true;
	_zombie setVariable ["CLY_removedead",false];
};

//Add magazine
if (random 1<=CLY_zombiemagazinechance or _armored) then {
	_mag=CLY_zombiemagazines select floor random count CLY_zombiemagazines;
	while {typeName _mag=="ARRAY"} do {
		_mag=_mag select floor random count _mag;
	};
	_zombie addMagazine _mag;
};

sleep random 1;

while {alive _zombie} do {
	///Idle///
	if ({isNil {_x getVariable "zombietype"}} count ((position _zombie) nearEntities [["Man","car"],300]) < 1) then {
		_zombie setdamage 1;
		diag_log format ["Zombie: %1 deleted!",_zombie];
	};
	while {isNull (_zombie getVariable "victim") and alive _zombie} do {
	

	};
	// Send this Victim variable to client and make the client run this code?
	///Combat///
	while {!isNull (_zombie getVariable "victim") and alive _zombie} do {
		_victim=_zombie getVariable "victim";
		_randomradius=0;
		_movepos=[(getPosATL vehicle _victim select 0)-_randomradius+random _randomradius*2,(getPosATL vehicle _victim select 1)-_randomradius+random _randomradius*2,getPosATL vehicle _victim select 2];
		_newmovepos=_movepos;
		_zombie doMove _movepos;
		_zombie lookAt _victim;
		
		_newmovedist=2.5*(_zombie distance vehicle _victim)*0.2;
		if (_newmovedist<2.5) then {_newmovedist=2.5};
		
		//Give up the chase
		if (!(_zombie getVariable "horde") and _zombie distance vehicle _victim>CLY_chasegiveupdist) then {_zombie setVariable ["victim",objNull]};
		
		//Chase
		while {!isNull (_zombie getVariable "victim") and alive _zombie and getPosATL vehicle _victim distance _movepos<_newmovedist and ((_zombie getVariable "horde") or _zombie distance vehicle _victim<CLY_chasegiveupdist)} do {
			if (_zombietype!="slow armored") then {_zombie setSpeedMode "FULL"} else {_zombie setSpeedMode "LIMITED"};
			if (_zombietype=="sneaker" and (_zombie distance _victim<25 or damage _zombie>0)) then {_zombie setUnitPos "UP"};
			if ({isNil {_x getVariable "zombietype"}} count ((position _zombie) nearEntities [["Man","car"],300]) < 1) then {
				_zombie setdamage 1;
				diag_log format ["Zombie: %1 deleted!",_zombie];
			};
			//Noise
			if (time>_noisewait) then {
				if (_zombietype=="normal" or _zombie distance _victim<4) then {
					_sound=CLY_noises_chase select floor random count CLY_noises_chase;
					_zombie say3D _sound;
					CLY_z_noisepv=[_zombie,_sound];
					publicVariable "CLY_z_noisepv";
					_noisewait=time+5+random 10;
				} else {
					if (_zombie distance _victim<12 and unitPos _zombie!="DOWN" and canStand _zombie) then {
						_sound=CLY_noises_chase select floor random count CLY_noises_chase;
						_zombie say3D _sound;
						CLY_z_noisepv=[_zombie,_sound];
						publicVariable "CLY_z_noisepv";
						_noisewait=time+5+random 10;
					};
				};
			};
			
			//Change victim
			if (time>_victimchangecooldown) then {
				_dist=_zombie distance vehicle _victim;
				_nearest=objNull;
				{
					if (
						_zombie distance vehicle _x<_dist and (
							_zombie distance vehicle _x<2.75
						 	or (_zombie knowsAbout _x>0 and (_zombie distance vehicle _x)*(_zombie distance vehicle _victim)^-1<0.5)
						)
					) then {
						_nearest=_x;
						_dist=_zombie distance vehicle _x;
					};
				} forEach CLY_zombievictims;
				if (!isNull _nearest) then {
					_victim=_nearest;
					_zombie setVariable ["victim",_victim];
					_victimchangecooldown=time+5;
				};
			};
			
			if (_zombie getVariable "victim"!=_victim) then {_victim=(_zombie getVariable "victim");_victimchangecooldown=time+10};
			if (!alive _victim or !(_victim in CLY_zombievictims)) then {_zombie setVariable ["victim",objNull]};
			
			//Attack
			if (!CLY_cutscene and alive _victim and ((_zombie distance vehicle _victim<2.75 and (getPosASL _victim select 2)-(getPosASL _zombie select 2)<1.5 and (getPosASL _victim select 2)-(getPosASL _zombie select 2)>-1) or (vehicle _victim!=_victim and _zombie distance vehicle _victim<6))) then {
				_anim="awoppercmstpsgthwnondnon_end";
				if ((_zombie selectionPosition "launcher" select 2)<0.5372) then {_anim="awopppnemstpsgthwnondnon_end"};
				_sound=CLY_noises_attack select floor random count CLY_noises_attack;
				_object="HeliHEmpty" createVehicleLocal [0,0,0];
				_object attachTo [_zombie,[0,0,1.5]];
				_object say3D _sound;
				[_object] spawn {sleep 2;deleteVehicle (_this select 0)};
				_zombie switchMove _anim;
				CLY_z_attackpv=[_zombie,_sound,_anim];
				publicVariable "CLY_z_attackpv";
				_zombie setVectorDir [(getPos vehicle _victim select 0)-(getPos _zombie select 0),(getPos vehicle _victim select 1)-(getPos _zombie select 1),0];
				
				_claw=1;
				if (damage _victim>0.28) then {_claw=2};
				if (damage _victim>0.59) then {_claw=3};
				
				if (vehicle _victim==_victim) then {
					_sound=CLY_noises_scream select floor random count CLY_noises_scream;
					if (typeOf _victim in ["Fin","Pastor"]) then {_sound=CLY_noises_yelp select floor random count CLY_noises_yelp};
					_object="HeliHEmpty" createVehicleLocal [0,0,0];
					_object attachTo [_victim,[0,0,1.5]];
					_object say3D _sound;
					[_object] spawn {sleep 5;deleteVehicle (_this select 0)};
					[_victim,_claw] call CLY_z_claw;
					CLY_z_victimpv=[_victim,_sound,_claw];
					publicVariable "CLY_z_victimpv";
					_victim setDamage (damage _victim+0.31);
					_entities = (position _victim) nearEntities ["Man",500];
					if ((damage _victim > .89) && 
                    ({isPlayer _X} count (_entities) < 2)) then {
						{
							sleep 20;
							_x setDamage 1
						} forEach _entities;
					};						
				} else {
					_damage=damage vehicle _victim+2/getNumber (configFile/"CfgVehicles"/(typeOf vehicle _victim)/"armor");
					if (_damage<=0.99) then {
						vehicle _victim setDamage _damage;
					} else {
						vehicle _victim setDamage 0.99;
						{_x action ["eject",vehicle _victim]} forEach crew vehicle _victim;
					};
				};
				sleep 1.5;
			};
			
			_newmovedist=2.5*(_zombie distance vehicle _victim)*0.2;
			if (_newmovedist<2.5) then {_newmovedist=2.5};
			
			sleep 1;
		};
	};
};

//Add bandage
if (_bandage and _zombie getVariable "victim"!=_zombie) then {
	_action=[
		format ["Take bandage",_leftstring],
		"zombie_scripts\cly_z_takebandage.sqs",
		1,
		1.4,
		true,
		true,
		"",
		"_this!=_target and !alive _target and _this distance _target<4 and _this getVariable 'CLY_healings'>=0 and !(_this isKindOf 'Animal')"
	];
	_zaction = _zombie addAction _action;
	CLY_healbandagepv=[_zombie,_action];
	publicVariable "CLY_healbandagepv";
	_h="HeliHEmpty" createVehicle getPos _zombie;
	_h attachTo [_zombie,[0,0,0]];
	
	sleep 15;
		
	_h=getPosATL _zombie nearObjects ["HeliHEmpty",0.1];
	if (count _h>0) then {deleteVehicle (_h select 0)};

	CLY_dontremovedeadremove set [count CLY_dontremovedeadremove,_zombie];
	publicVariable "CLY_dontremovedeadremove";
	_zombie removeAction _zaction;
	deleteVehicle _zombie;

};

if (_zombie getVariable "victim"==_zombie) then {
	deleteVehicle _zombie;
};